// examples/lazy_pull.go
// 懒加载拉取示例

package examples

import (
	"context"
	"log"

	"github.com/onyx/snapshotter/pkg/registry"
)

func ExampleLazyPull() error {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
	defer cancel()

	// 解析镜像引用
	imageRef := "docker.io/library/ubuntu:22.04"
	ref, err := registry.ParseImageRef(imageRef)
	if err != nil {
		return err
	}

	log.Printf("Parsed ref: registry=%s, repo=%s, tag=%s",
		ref.Registry, ref.Repo, ref.Tag)

	// 创建注册表客户端
	client := registry.NewClient()

	// 拉取清单
	manifest, err := client.PullManifest(ctx, ref)
	if err != nil {
		return err
	}

	log.Printf("Manifest: %d layers, size=%d bytes",
		len(manifest.Layers), manifest.Config.Size)

	// 对每个层执行懒加载
	for i, layer := range manifest.Layers {
		// 只拉取元数据，延迟实际数据
		log.Printf("Layer %d: %s (%d bytes)", i, layer.Digest[:16], layer.Size)

		// 在实际使用时才下载数据
		// data, err := client.PullLayerPartial(ctx, ref, layer.Digest, 0, 1024)
	}

	return nil
}
